package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Product;
import com.itheima.service.ProductSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(path = "/product")
public class ProductController {
    @Autowired
    private ProductSercice productSercice;

    @RequestMapping(path = "/findAll")
    public String findAll(Model model, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "pageSize", required = true, defaultValue = "4") int pageSize) {

        List<Product> list = productSercice.findAllProduct(page, pageSize);

        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("productList", pageInfo);
        return "product-list";
    }

//        @RequestMapping(path = "/save")
//    public String saveProduct(Product product,Model model) {
//        System.out.println("劲来了");
//        productSercice.saveProduct(product);
//        String all = this.findAll(model,1,4);
//        return all;
//    }
//    @RequestMapping(path = "/save")
//    public String saveProduct(@RequestParam(name = "productName") String productName,
//                              @RequestParam(name = "departureTime") String departureTime,
//                              @RequestParam(name = "cityName") String cityName,
//                              @RequestParam(name = "productPrice") String productPrice,
//                              @RequestParam(name = "productStatus") String productStatus,
//                              @RequestParam(name = "productDesc") String productDesc,
//                              Model model) {
//        System.out.println(productName + "=" + departureTime + "=" + cityName + "=" + productPrice + "=" + productStatus + "=" + productDesc);
//        System.out.println("劲来了");
////        productSercice.saveProduct(product);
//        String all = this.findAll(model, 1, 4);
//        return all;
//    }
    @RequestMapping(path = "/save")
    public String saveProduct(Product product) {
        System.out.println("劲来了");
        int row = productSercice.saveProduct(product);
        if (row == 0) {
            return "product-add";
        }
        return "redirect:/product/findAll";
    }

}
